class WelcomeController < ApplicationController
  before_filter :login_required
  
  def index
    u = session['user']
    id = u.id
    
    # clean up tasks
    #Task.find(:all,:conditions=>"project_id=NULL AND title=''").each { |t| logger.debug("delete #{t.id}") } #Task.delete(t.id)
    
    # invitations
    @invit = Invitation.find_all_by_email(u.email)
    if(u.projects.size==0 and @invit != nil) # accept automatically
      u.accept_invitation(@invit)
      @invit = []
      flash[:notice] = "You've just joined the projects you've been invited to. Create a new task now by clicking on tasks, in the menu above."
    end
    
    # view
    @projects        = u.projects(true).sort_by {|p| [p.name]}
    @important_tasks = Task.find(:all, :conditions=>"done='0' and user_id='#{id}' and (project_id!=NULL or title!='')", :order=>"priority,`order`", :limit=>10)
  end
  
end
